/*
Copyright 2009 Jeff Carnes

This file is part of Fluant.

Fluant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

Fluant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.amiasleep.fluant.flaPublisher.io.parser;

import java.io.File;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import com.amiasleep.fluant.flaPublisher.io.SWFOutput;



public class SWFParser {
	
	
	public static SWFOutput fromXml(Document xml)
	{
		SWFOutput retVal = null;
		
		try
		{
			XPathFactory factory = XPathFactory.newInstance();
			XPath xpath = factory.newXPath();
			double doOutput = (Double) xpath.evaluate("/flash_profile/PublishFormatProperties/flash", xml, XPathConstants.NUMBER);
			
			
			if(doOutput == 1)
			{
				retVal = new SWFOutput();
				
				String name = xpath.evaluate("/flash_profile/PublishFormatProperties/flashFileName", xml);
				File file = new File(name);
				retVal.setFile(file);
				
				double topDown = (Double) xpath.evaluate("/flash_profile/PublishFlashProperties/TopDown", xml, XPathConstants.NUMBER);
				retVal.setTopDown( topDown == 1 );
				
				double report = (Double) xpath.evaluate("/flash_profile/PublishFlashProperties/Report", xml, XPathConstants.NUMBER);
				retVal.setGenerateReport( report == 1 );
				
				double protect = (Double) xpath.evaluate("/flash_profile/PublishFlashProperties/Protect", xml, XPathConstants.NUMBER);
				retVal.setProtectFromImport( protect == 1 );
				
				double omitTraceActions = (Double) xpath.evaluate("/flash_profile/PublishFlashProperties/OmitTraceActions", xml, XPathConstants.NUMBER);
				retVal.setOmitTraceActions( omitTraceActions == 1);
				
				Double jpegQuality = (Double) xpath.evaluate("/flash_profile/PublishFlashProperties/Quality", xml, XPathConstants.NUMBER);
				retVal.setJpegQuality( jpegQuality.shortValue() );
				
				double debug = (Double) xpath.evaluate("/flash_profile/PublishFlashProperties/DebuggingPermitted", xml, XPathConstants.NUMBER);
				retVal.setPermitDebugging( debug == 1 );
			}
		}
		catch(XPathException xpex)
		{
			xpex.printStackTrace();
		}
		
		return retVal;
	}

}
